<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class api_mod extends module
{
    function __construct()
    {
        $this->uid = @$_SESSION['user']['uid'];
        parent::__construct();
    }
    function notice($type, $tpl_name, $arr)
    {
        $query = $this->notice_sms($row['username'], $row['tpl_name'], $arr);
        if ($type == 'sms' || $type == 'email' || $type == 'wxtpl') {
            if (!empty($this->uid)) {
                $setarr['uid'] = $this->uid;
            } else {
                $setarr['uid'] = '0';
            }
            $setarr['type'] = $type;
            $setarr['tpl_name'] = $tpl_name;
            $setarr['username'] = $arr['username'];
            unset($arr['username']);
            $setarr['content'] = serialize($arr);
            $setarr['addtime'] = time();
            $query = $this->db->add(table('notice'), $setarr);
            if ($query) {
                $insert_id = $this->db->insert_id();
                curl::asynGET("index.php?m=ajax&a=notice_run&id=" . $insert_id);
            }
        } else {
            return false;
        }
    }
    function notice_run($id)
    {
        $row = $this->db->get_one("select * from " . table('notice') . " where status = 0 and id = " . $id);
        if ($row['id']) {
            $arr = unserialize($row['content']);
            if ($row['type'] == 'sms') {
                $query = $this->notice_sms($row['username'], $row['tpl_name'], $arr);
            } elseif ($row['type'] == 'wxtpl') {
                $query = $this->notice_wxtpl($row['username'], $row['tpl_name'], $arr);
            } elseif ($row['type'] == 'email') {
                $query = $this->notice_email($row['username'], $row['tpl_name'], $arr);
            }
            if ($query['error'] == '0') {
                $starr['status'] = '1';
                $query = $this->db->update(table('notice'), $starr, 'id = ' . $id);
                if ($query) {
                    return true;
                } else {
                    return false;
                }
            } else {
                $starr['status'] = '2';
                $starr['remark'] = $query['msg'];
                $query = $this->db->update(table('notice'), $starr, 'id = ' . $id);
                if ($query) {
                    return true;
                } else {
                    return false;
                }
            }
        }
    }
    function notice_sms($mobile, $tpl_name, $arr)
    {
        $an_row = $this->db->get_one("select * from " . table("api_notice") . " where alias = '" . $tpl_name . "' limit 1");
        if ($an_row) {
            $sms_row = $this->sms_row();
            $wsql = " and type = 'sms' and type2 = '" . $sms_row['alias'] . "'";
            $tpl_row = $this->db->get_one("select * from " . table("api_notice_tpl") . " where nid = '" . $an_row['id'] . "' " . $wsql . " limit 1");
            if ($tpl_row['id']) {
                if ($tpl_row['status'] == '1') {
                    $class_name = 'sms_' . $sms_row['alias'];
                    $sms = new $class_name($sms_row['app_key'], $sms_row['app_secret']);
                    $aaa = explode(',', $tpl_row['param']);
                    $aaa2 = explode(',', $tpl_row['param2']);
                    foreach ($aaa2 as $kk => $vv) {
                        $parr[$vv] = $arr[$aaa[$kk]];
                    }
                    if ($sms_row['sign'] == '' || $sms_row['app_key'] == '' || $sms_row['app_secret'] == '') {
                        return array('error' => '1', 'msg' => $sms_row['alias'] . 'APP KEY, APP SECRET, SIGN签名不能为空');
                    } else {
                        if (isset($parr)) {
                            $exec = $sms->send($mobile, $parr, $tpl_row['tpl_id'], $sms_row['sign']);
                            if ($exec['error'] == '0') {
                                return array('error' => '0', 'msg' => '发送成功');
                            } else {
                                return array('error' => '1', 'msg' => $exec['msg']);
                            }
                        } else {
                            return array('error' => '1', 'msg' => '系统提交参数错误');
                        }
                    }
                } else {
                    return array('error' => '1', 'msg' => '短信未开启');
                }
            } else {
                return array('error' => '1', 'msg' => '短信配置不存在');
            }
        } else {
            return array('error' => '1', 'msg' => '短信模块不存在');
        }
    }
    function notice_wxtpl($openid, $tpl_name, $arr)
    {
        $wsql = " and type = 'wxtpl'";
        $an_row = $this->db->get_one("select * from " . table("api_notice") . " where alias = '" . $tpl_name . "' limit 1");
        if ($an_row) {
            $tpl_row = $this->db->get_one("select * from " . table("api_notice_tpl") . " where nid = '" . $an_row['id'] . "' " . $wsql . " limit 1");
        }
        $aaa = explode(',', $tpl_row['param']);
        $aaa2 = explode(',', $tpl_row['param2']);
        preg_match_all('#{([^}]*)#i', $tpl_row['content'], $matches);
        $aaaa = $matches[1];
        foreach ($aaa2 as $kk => $vv) {
            $dat[$vv] = array('value' => isset($arr[$aaa[$kk]]) ? $arr[$aaa[$kk]] : $aaaa[$kk], 'color' => '#666666');
        }
        $data['touser'] = $openid;
        $data['url'] = $arr['link'];
        $data['template_id'] = $tpl_row['tpl_id'];
        $data['data'] = $dat;
        $access_token = wchat::init()->access_token();
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $access_token;
        $postdata = (object) $data;
        $json_data = urldecode(json_encode($postdata));
        $result = curl::post($url, $json_data);
        $result = json_decode($result);
        if ($result->errcode == '0') {
            return array('error' => '0', 'msg' => '发送成功');
        } else {
            return array('error' => '1', 'msg' => $result->errmsg);
        }
    }
    function notice_email($email, $tpl_name, $arr)
    {
        $an_row = $this->db->get_one("select * from " . table("api_notice") . " where alias = '" . $tpl_name . "' limit 1");
        if ($an_row) {
            $wsql = " and type = 'email'";
            $tpl_row = $this->db->get_one("select * from " . table("api_notice_tpl") . " where nid = '" . $an_row['id'] . "' " . $wsql . " limit 1");
        }
        $aaa = explode(',', $tpl_row['param']);
        $aaa2 = explode(',', $tpl_row['param2']);
        $content = $tpl_row['content'];
        foreach ($aaa2 as $kk => $vv) {
            $value = @$arr[$aaa[$kk]];
            $content = str_replace('{' . $vv . '}', $value, $content);
        }
        $query = $this->email($email, $tpl_row['name'], $content);
        if ($query) {
            return array('error' => '0', 'msg' => '发送成功');
        } else {
            return array('error' => '1', 'msg' => '发送失败');
        }
    }
    function notice_list($tpl_name, $cname)
    {
        $notice_name = "config/api_notice_" . $tpl_name;
        $cache_arr = cache::get($notice_name);
        if (!$cache_arr) {
            $row = $this->db->get_one("select * from " . table('api_notice') . " where alias = '" . $tpl_name . "'");
            $query = $this->db->get_all("select * from " . table('api_notice_tpl') . " where nid = " . $row['id'] . " and type = " . $cname . " and status = 1");
            if ($query) {
                foreach ($query as $k => $v) {
                    $data[] = $v;
                }
                if ($data) {
                    cache::add($notice_name, $data);
                } else {
                    $data = array();
                    cache::add($notice_name, $data);
                }
                return $data;
            } else {
                return false;
            }
        } else {
            return $cache_arr;
        }
    }
    function sms_row()
    {
        $sms_name = "config/api_sms";
        $cache_arr = cache::get($sms_name);
        if (!$cache_arr) {
            $sms_row = $this->db->get_one("select * from " . table('api_sms') . " where status = 1");
            cache::add($sms_name, $sms_row);
            return $sms_row;
        } else {
            return $cache_arr;
        }
    }
    function login_list($arr)
    {
        $cache_arr = cache::get("config/api_login");
        if (!$cache_arr) {
            $cache_arr = $this->db->get_all("select * from " . table('api_login') . " where status = 1");
            if ($cache_arr) {
                cache::add("config/api_login", $cache_arr);
            }
        }
        $user_status = isset($arr['user_status']) ? $arr['user_status'] : false;
        if ($user_status) {
            $row = $this->db->get_one("select uid,qq_uid,weibo_uid,wchat_uid from " . table('member') . " where uid = " . $this->uid);
            foreach ($cache_arr as $k => $v) {
                $apiname = $v['alias'] . '_uid';
                if ($row[$apiname]) {
                    $v['bind_status'] = '1';
                }
                $data[] = $v;
            }
            return $data;
        } else {
            return $cache_arr;
        }
    }
    function email($from_to, $subject = null, $body = null, $attachment = null)
    {
        require UQCMS_PATH . 'UQframework/phpmailer/class.phpmailer.php';
        $mail = new PHPMailer();
        $mail->CharSet = 'UTF-8';
        $mail->IsSMTP();
        $mail->SMTPDebug = 0;
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = 'ssl';
        $mail->Host = cfg('email_host');
        $mail->Port = cfg('email_port');
        $mail->Username = cfg('email_username');
        $mail->Password = cfg('email_password');
        $mail->SetFrom(cfg('email_username'), cfg('email_name'));
        $mail->Subject = $subject;
        $mail->MsgHTML($body);
        $mail->AddAddress($from_to);
        if ($mail->Send()) {
            return true;
        } else {
            return false;
        }
    }
    public function notice_type($str = null)
    {
        $arr = array('wxtpl' => '微信模板', 'sms' => '短信通知', 'email' => '邮件通知');
        if ($str) {
            return $arr[$str];
        } else {
            return $arr;
        }
    }
}